package com.ask.forMe.controller.business;

import com.ask.forMe.model.dto.ChatMessages;
import com.ask.forMe.model.entity.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/read")
@Tag(name = "数据库读写性能测试接口")
public class ReadCapController {
    private static final String REDIS_KEY_PREFIX = "chat_memory:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("/redis")
    @Operation(summary = "从redis中读取")
    private Result readFromRedis(String memoryId) {
        String cache = stringRedisTemplate.opsForValue().get(REDIS_KEY_PREFIX + memoryId);
        return Result.success(cache);
    }

    @GetMapping("/mongoDb")
    @Operation(summary = "从mongoDB中读取")
    private Result readFromMongoDB(String memoryId) {
        Criteria criteria = Criteria.where("memoryId").is(memoryId);
        Query query = new Query(criteria);
        ChatMessages chatMessages = mongoTemplate.findOne(query, ChatMessages.class);
        //List<ChatMessage> messagesFromJson = ChatMessageDeserializer.messagesFromJson(chatMessages.getContent());
        return Result.success(chatMessages.getContent());
    }
}
